package com.mcc.app.process.service.impl;

import com.mcc.app.cache.domain.PlcHisValueInfo;
import com.mcc.app.cache.service.IPlcCacheService;
import com.mcc.app.process.domain.Bellows;
import com.mcc.app.process.mapper.BellowsMapper;
import com.mcc.app.process.service.IBellowsService;
import com.mcc.common.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 风箱Service业务层处理
 *
 * @author sg
 * @date 2021-09-03
 */
@Service
public class BellowsServiceImpl implements IBellowsService
{
    @Autowired
    private BellowsMapper bellowsMapper;


    @Autowired
    private IPlcCacheService plcCacheService;

    /**
     * 查询风箱
     *
     * @param id 风箱ID
     * @return 风箱
     */
    @Override
    public Bellows selectBellowsById(Long id)
    {
        return bellowsMapper.selectBellowsById(id);
    }

    /**
     * 查询风箱列表
     *
     * @param bellows 风箱
     * @return 风箱
     */
    @Override
    public List<Bellows> selectBellowsList(Bellows bellows)
    {
        List<Bellows> bellowsList = bellowsMapper.selectBellowsList(bellows);
        for (Bellows bellows1:bellowsList){
            bellows1.setTemperatureCA(bellows1.getTemperatureA());
            bellows1.setTemperaturecB(bellows1.getTemperatureB());
            bellows1.setPressurecA(bellows1.getPressureA());
            bellows1.setPressurecB(bellows1.getPressureB());
            bellows1.setTemperatureA(plcCacheService.getCacheDataToStr(bellows1.getTemperatureA()));
            bellows1.setTemperatureB(plcCacheService.getCacheDataToStr(bellows1.getTemperatureB()));
            if(bellows1.getTemperatureC()!=null){
                bellows1.setTemperaturecC(bellows1.getTemperatureC());
                bellows1.setTemperatureC(plcCacheService.getCacheDataToStr(bellows1.getTemperatureC()));
            }
            bellows1.setPressureA(plcCacheService.getCacheDataToStr(bellows1.getPressureA()));
            bellows1.setPressureB( plcCacheService.getCacheDataToStr(bellows1.getPressureB()));
        }
        return bellowsList;
    }

    /**
     * 新增风箱
     *
     * @param bellows 风箱
     * @return 结果
     */
    @Override
    public int insertBellows(Bellows bellows)
    {
        return bellowsMapper.insertBellows(bellows);
    }

    /**
     * 修改风箱
     *
     * @param bellows 风箱
     * @return 结果
     */
    @Override
    public int updateBellows(Bellows bellows)
    {
        return bellowsMapper.updateBellows(bellows);
    }

    /**
     * 批量删除风箱
     *
     * @param ids 需要删除的风箱ID
     * @return 结果
     */
    @Override
    public int deleteBellowsByIds(Long[] ids)
    {
        return bellowsMapper.deleteBellowsByIds(ids);
    }

    /**
     * 删除风箱信息
     *
     * @param id 风箱ID
     * @return 结果
     */
    @Override
    public int deleteBellowsById(Long id)
    {
        return bellowsMapper.deleteBellowsById(id);
    }

    @Override
    public List<PlcHisValueInfo> getBellowDataDialog(String bellowId, String startDt, String endDt) {

        List<PlcHisValueInfo> plcHisList = new ArrayList<>();
        Bellows bellow;
        if (bellowId == null){
            List<Bellows> bellows = bellowsMapper.selectBellowsList(new Bellows());
            bellow = bellows.get(0);
            if (bellow == null){
                throw new BaseException("无风箱数据");
            }
        }else {
             bellow = selectBellowsById(Long.parseLong(bellowId));
        }

        /*PlcHisValueInfo plcPressureAInfo = plcHistoryInfo.getPlcHisValuesInfo(bellow.getPressureA(), startDt, endDt);
        plcPressureAInfo.setTypeName("压力");
        plcHisList.add(plcPressureAInfo);
        PlcHisValueInfo plcPressureBInfo = plcHistoryInfo.getPlcHisValuesInfo(bellow.getPressureB(), startDt, endDt);
        plcPressureBInfo.setTypeName("压力");
        plcHisList.add(plcPressureBInfo);
        PlcHisValueInfo plcTemperatureAInfo = plcHistoryInfo.getPlcHisValuesInfo(bellow.getTemperatureA(), startDt, endDt);
        plcTemperatureAInfo.setTypeName("温度");
        plcHisList.add(plcTemperatureAInfo);
        PlcHisValueInfo plcTemperatureBInfo = plcHistoryInfo.getPlcHisValuesInfo(bellow.getTemperatureB(), startDt, endDt);
        plcTemperatureBInfo.setTypeName("温度");
        plcHisList.add(plcTemperatureBInfo);*/
        return plcHisList;
    }

    @Override
    public Bellows getBellowsDataNow(String bellowId) {
        Bellows bellows = new Bellows();
        if(null == bellowId){
            return bellows;
        }

        String pN = "st3_fxfqw" + bellowId + "_p";
        String pS = "st3_fxfqo" + bellowId + "_p";
        String tS = "st3_fxfqw" + bellowId + "_t";
        String tN = "st3_fxfqo" + bellowId + "_t";

        String bellowPn = plcCacheService.selectPlcCacheByCode(pN).getCacheValue();
        String bellowPs = plcCacheService.selectPlcCacheByCode(pS).getCacheValue();
        String bellowTn = plcCacheService.selectPlcCacheByCode(tS).getCacheValue();
        String bellowTs = plcCacheService.selectPlcCacheByCode(tN).getCacheValue();

        bellows.setBellowsPressureN(bellowPn);
        bellows.setBellowsPressureS(bellowPs);
        bellows.setBellowsTempN(bellowTn);
        bellows.setBellowsTempS(bellowTs);

        return bellows;
    }
}
